package com.example.mallsystem1104.controller;

import com.example.mallsystem1104.annotation.Authority;
import com.example.mallsystem1104.common.Result;
import com.example.mallsystem1104.entity.AuthorityType;
import com.example.mallsystem1104.entity.Cart;
import com.example.mallsystem1104.service.CartService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author zy
 * @Date 2024/12/12 11:07
 * @packageName:com.example.mallsystem1104.controller
 * @ClassName:CartController
 */
@RestController
@RequestMapping("/api/cart")
@CrossOrigin
@Authority(AuthorityType.requireLogin)
public class CartController {

    @Resource
    private CartService cartService;

    @GetMapping("{id}")
    public Result selectById(@PathVariable Long id){
       return Result.success(cartService.getById(id));
    }
    /**
     * 查询所有用户的购物车信息
     * @return
     */
    @GetMapping
    public Result findAll(){
        return Result.success(cartService.list());
    }

    /**
     * 查询某个用户的购物车
     * @param userId
     * @return
     */
    @GetMapping("/userid/{userId}")
    public Result selectByUserId(@PathVariable Long userId){
        return Result.success(cartService.selectByUserId(userId));
    }

    @PostMapping
    public Result save(@RequestBody Cart cart){
        cartService.saveOrUpdate(cart);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody Cart cart){
        cartService.updateById(cart);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable long id){
        cartService.removeById(id);
        return Result.success();
    }
}
